<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const ws = new WebSocket('ws://localhost:8081')
        // 当连接打开时，向服务器发送一些数据
        ws.onopen = () => {
            ws.send('Hi!服务端') // 使用send方法向服务端发送内容
        }

        // 监听错误
        ws.onerror = error => {
            console.log('WebSocket Error ' + error);
        };

        // 监听服务端发送的数据
        ws.onmessage = e => {
            console.log('Server: ' + e.data);
        };
    </script>
</body>

</html>